Skip to content

Fix #49: Add Scala.js support #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 4, 2017
Merged

Conversation

OlivierBlanvillain
Copy link
Contributor

This PR adds Scala.js support.

The changes are pretty straightforward. After updating the bulid, a52ca9c moves files around to cope with the Scala.js jvm/js/shared folder structure. Almost everything goes to /shared, except for the few test cases that lead to linking errors on Scala.js. Then fc86cb2 copies the problematic tests to /js and manually comments the parts involving JVM specific stuff such as accesses to the file system.

I think the prime use case for xml literals in Scala.js is to generate equivalent org.scalajs.dom.raw.Node, which does not require any of the JVM specific parts of the library. Binding.scala and scala-js-react are already doing this by the mean of macros: they get ride of everything coming from scala.xml._ at compile time. This approach unfortunately leads to long compilation times. Using xml literals natively from Scala.js (probably with a run time transformation to org.scalajs.dom.raw.Node) would leads to much better compilation time.

@biswanaths
Copy link
Contributor

Thanks @OlivierBlanvillain. I will take a look at the build failure https://travis-ci.org/scala/scala-xml/builds/163345736. And let the community take a look at this too.

@biswanaths
Copy link
Contributor

biswanaths commented Sep 29, 2016

Build seems fine https://travis-ci.org/scala/scala-xml/builds/163353923. There seems intermittent download error.

@OlivierBlanvillain
Copy link
Contributor Author

Yes, I rolled back the version of sbt (I updated it without a good reason anyway), the problem seems to come from repo.typesafe.com (sbt/sbt#2758).

@SethTisue
Copy link
Member

SethTisue commented Sep 29, 2016

I rolled back the version of sbt (I updated it without a good reason anyway)

I'd encourage resubmitting that part once the sbt/sbt#2758 storm has passed

UPDATE (Oct 3): That storm has passed.

@olafurpg
Copy link

Wow, this would be a huge enabler for frontend Scala.js development. We are currently using Binding.scala in a medium sized project. Our designer loves using HTML syntax but he hates the abominable compilation times caused by the macro annotations that eliminate scala.xml._.

One question, it seems XML.load is commented out in all the js/ tests, presumably because it requires JVM specific APIs. Will there be a js alternative to Xml.load?

@OlivierBlanvillain
Copy link
Contributor Author

OlivierBlanvillain commented Sep 30, 2016

@olafurpg Thanks for your feedback! XML.load relies on SAXParser which is JVM only. The best option would be to switch to a pure Scala XML parser, this fastparse implementation for example.

@deontologic
Copy link

I look forward to this being merged :)

@biswanaths
Copy link
Contributor

biswanaths commented Oct 13, 2016

What would be the right way to test this ? Any ideas ?

For a starter I will start with a basic scala.js app and try to use scala-xml and see how that pans out.

@sjrd
Copy link
Member

sjrd commented Oct 13, 2016

What would be the right way to test this ?

Hum ... xmlJS/test?

@biswanaths
Copy link
Contributor

biswanaths commented Oct 13, 2016

@sjrd thank you for pitching in. Really appreciate your views regarding scalajs support.

I have gone through your post http://stackoverflow.com/questions/30143126/how-do-i-port-an-existing-scala-library-to-scalajs/30144349#30144349 and http://www.scala-js.org/doc/project/cross-build.html .

To be clearer, my question would have been is this sufficient or do we need to have any more tests, to make sure the library works and behaves well when used with scala.js.

@sjrd
Copy link
Member

sjrd commented Oct 13, 2016

It is as sufficient as the tests for the JVM. Everything that should work on both platform should be tested in shared/src/test/. They will be tested with xmlJVM/test and xmlJS/test. I'm not sure what "more" you want. If you want more tests, you can add more tests, but that's just like on the JVM.

@biswanaths
Copy link
Contributor

@sjrd thanks for quick reply and regarding tests I got it. I think we should be good enough to go on this then.

.jsConfigure(_.enablePlugins(ScalaJSJUnitPlugin))

lazy val xmlJVM = xml.jvm
lazy val xmlJS = xml.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cleanup of the build.sbt file and modernization for other compilers is great.

Copy link
Member

@ashawley ashawley Oct 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the publish-signed task work with the new changes to SBT? The admin/build.sh file is run by Travis, and when it's for a Git tag, it will copy the admin/publish-settings.sbt file to the root directory. Looks like it will cause SBT to fail to load:

$ env PGP_PASSPHRASE=foobar sbt
[info] Loading project definition from scala-xml/project
scala-xml/publish-settings.sbt:3: error: not found: value pgpPassphrase
pgpPassphrase := Some(env("PGP_PASSPHRASE").toArray)
^

@@ -0,0 +1,202 @@
// package scala.xml
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is no plan to fix the tests, maybe just don't copy them over? Just remove them from the js/src/test

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I agree.

// Elem(null, "title", e, sc, Text("Foundations of Programming Languages"))),
// Elem(null, "author", e, sc, Text("John Mitchell")),
// Elem(null, "title", e, sc, Text("Foundations of Programming Languages"))))
// }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, if no plan to support these tests for the ScalaJS test suite, then just delete instead of comment out?

* uses as many old nodes as possible.
*
* Three transformers class for case -
* One for orginal, one for modified, and one proposed which shows
* all are equivalent when it comes to reusing as many nodes as possible
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the only changes to this file, ReuseNodesTests.scala, are changes to whitespace, maybe revert so as to avoid touching the file besides the rename. We can fix whitespace in another PR

@@ -96,7 +97,7 @@ abstract class CachedFileStorage(private val file1: File) extends Thread {
val c = fos.getChannel()

// @todo: optimize
val storageNode = <nodes>{ nodes.toList }</nodes>
val storageNode = <nodes>{nodes.toList}</nodes>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file, CachedFileStorage.scala, also only has whitespace changes that should be preserved for now, IMO

Copy link
Member

@ashawley ashawley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that scalajs needs the directory structure to be divided by jvm and js and shared. This is like the 3rd time the directory structure will change significantly for this long living package. This would be a good time to get it right, since the current structure frustrates me since it is overly long. This PR will make it longer. Sorry to attempt to tack these directory issues on to a PR for ScalaJS support, but so it goes.

Currently, it is

src/main/scala/scala/xml/
src/test/scala/scala/xml/

It would be better if it was this, IMO

src/main/scala/
src/test/scala/

For this ScalaJS PR, the current change renames things to

xml/js/src/test/scala/scala/xml/
xml/jvm/src/test/scala/scala/xml/
xml/shared/src/main/scala/scala/xml/
xml/shared/src/test/scala/scala/xml/
js/src/test/scala/
jvm/src/test/scala/
shared/src/main/scala/
shared/src/test/scala/

I don't have Scalajs experience, so I apologize for making suggestions out of ignorance.

@OlivierBlanvillain
Copy link
Contributor Author

@ashawley thanks for you detailed review! I've forced push 3 updated commits which address all of your comment. Regarding the publish-signed did you forgot the cat admin/gpg.sbt >> project/plugins.sbt part?

For reference, I managed to sbt publish-signed this branche as "in.nvilla" %%% "scala-xml" % "1.0.6" using this build.sbt.

@sjrd
Copy link
Member

sjrd commented Oct 14, 2016

It would be better if it was this, IMO
src/main/scala/
src/test/scala/

Whether good or bad, this would go against commonly acknowledged practices. Moreover, this would confuse some IDEs.

For the Scala.js thing, it might be better to achieve this:

js/src/test/scala/scala/xml/
jvm/src/test/scala/scala/xml/
shared/src/main/scala/scala/xml/
shared/src/test/scala/scala/xml/

which can be done with lazy val xml = crossProject.in(file(".")).... This removes the unnecessary xml/ prefix to all the paths.

@ashawley
Copy link
Member

ashawley commented Oct 14, 2016

@OlivierBlanvillain Cool! Looks great, thanks. Yes, I missed that part of the build.sh script for publish-signed.

@sjrd I'm familiar with the convention, but wasn't aware the folder structure was a requirement for IDEs. I had assumed they use code indexing and such -- regardless of folder names to resolve things. I'm an SBT and Emacs scala-mode person, so if it's an issue I'd have to defer to others who use such IDEs.

@OlivierBlanvillain
Copy link
Contributor Author

Alright, structure is now as follows:

js/src/test/scala/scala/xml/
jvm/src/test/scala/scala/xml/
shared/src/main/scala/scala/xml/
shared/src/test/scala/scala/xml/

@OlivierBlanvillain
Copy link
Contributor Author

Rebased. Do you think this could get merged before 1.0.6?

@olafurpg
Copy link

FWIW, here is a demonstration of what's possible with scala-xml on Scala.js: https://olivierblanvillain.github.io/monadic-html/examples/

The demo uses a fork of scala-xml, it would be nice to depend on the official scala-xml module.

@SethTisue
Copy link
Member

@biswanaths @ashawley shall we merge?

@ashawley
Copy link
Member

LGTM. I appreciate @OlivierBlanvillain's willingness to minimize the file changes, and for entertaining my directory renaming wishes: Even if in the end, my whims were misguided.

I assume adding Scalacheck in #110 will require a similar pattern of disabling certain tests for ScalaJS?

@sjrd
Copy link
Member

sjrd commented Nov 29, 2016

I assume adding Scalacheck in #110 will require a similar pattern of disabling certain tests for ScalaJS?

Correct.

@biswanaths
Copy link
Contributor

@SethTisue , sure enough.

I think community had enough time to go through this.

I will go through the changes once again. And pull it in.

@bbarker
Copy link

bbarker commented Jan 8, 2017

Any other issues with this? Would really like to see it merged.

@bbarker
Copy link

bbarker commented Feb 4, 2017

@biswanaths @SethTisue @ashawley Nudge, anything I/we can do to help get this in?

I'd like to see it for scala.js, and once this is in, maybe experiment with using it in scala-native as well.

@SethTisue SethTisue merged commit 00fa469 into scala:master Feb 4, 2017
@SethTisue
Copy link
Member

SethTisue commented Feb 4, 2017

(the Scala team doesn't need to be the gatekeeper on everything in this repo, but this an especially juicy PR and there seems to be a consensus on merging it, and certainly plenty of review time passed, so I went ahead in this case)

@ashawley
Copy link
Member

ashawley commented Feb 4, 2017

Travis build error for Java 8 and Scala 2.12:

unresolved dependency: org.scala-js#scalajs-junit-test-plugin_2.12.1;0.6.12: not found

@SethTisue
Copy link
Member

huh, I don't know how I missed that, I thought I looked at the Travis results, maybe they were somehow not current. I'll prepare a followup PR right now with a fix.

SethTisue added a commit to SethTisue/scala-xml that referenced this pull request Feb 4, 2017
this is required to fix Travis failures after scala#109 was merged
@SethTisue SethTisue mentioned this pull request Feb 4, 2017
@SethTisue
Copy link
Member

thank you, Olivier, for this truly substantial contribution!

@ashawley ashawley mentioned this pull request Feb 4, 2017
@ashawley
Copy link
Member

ashawley commented Feb 4, 2017

So will tests need to be added to both JS and JVM trees going forward? Was there an option to have SBT run common tests between both Scala compilers?

@OlivierBlanvillain
Copy link
Contributor Author

@ashawley Cross platform tests should go in shared/src/test, jvm only tests in jvm/src/test. This PR introduced some duplication in tests which is a mistake. Everything in js/src/test should have been in shared/src/test, and jvm/src/test should only contains tests that are not in shared.

@ashawley
Copy link
Member

ashawley commented Feb 4, 2017

That's right, shared. Ok, the duplication should be weeded out.

@OlivierBlanvillain
Copy link
Contributor Author

OlivierBlanvillain commented Feb 4, 2017

@ashawley Let me open a new PR doing that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants